#include "dbsystemlog.h"
#include "dbapp.h"
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>

using namespace ns_yht;

DbSystemLog::DbSystemLog()
{
}

DbSystemLog::~DbSystemLog()
{
}

bool DbSystemLog::getList(QVector<QSharedPointer<ModelSystemLog>> &modelList)
{
    if (!checkTable())
        return false;
    QSqlQuery query(DATABASE);
    query.exec(QString("select id,stype,stime,remark from TSystemLog order by stime DESC "));
    if (!query.exec())
    {
        DBAPP->showErrorInfo("DbSystemLog::getList", query);
        return false;
    }
    else
    {
        modelList.clear();
        while (query.next())
        {
            QSharedPointer<ModelSystemLog> model(new ModelSystemLog);
            model->id = query.value("id").toInt();
            model->stype = query.value("stype").toInt();
            model->stime = query.value("stime").toLongLong();
            model->remark = query.value("remark").toString();
            modelList.append(model);
        }
        return true;
    }
}

bool DbSystemLog::getUnPostedList(QVector<QSharedPointer<ModelSystemLog>> &modelList)
{
    if (!checkTable())
        return false;
    QSqlQuery query(DATABASE);
    query.exec(QString("select id,stype,stime,remark from TSystemLog where posted=0"));
    if (!query.exec())
    {
        DBAPP->showErrorInfo("DbSystemLog::getUnPostedList", query);
        return false;
    }
    else
    {
        modelList.clear();
        while (query.next())
        {
            QSharedPointer<ModelSystemLog> model(new ModelSystemLog);
            model->id = query.value("id").toInt();
            model->stype = query.value("stype").toInt();
            model->stime = query.value("stime").toLongLong();
            // model->remark = query.value("remark").toString();
            modelList.append(model);
        }
    }
    return true;
}

bool DbSystemLog::updatePosted()
{
    if (!checkTable())
        return false;
    QSqlQuery query(DATABASE);
    query.prepare("update TSystemLog set posted=1 where posted=0");
    if (!query.exec())
    {
        DBAPP->showErrorInfo("DbSystemLog::updatePosted", query);
        return false;
    }
    return true;
}

bool DbSystemLog::getList(QVector<QSharedPointer<ModelSystemLog>> &modelList, qint64 startTime, qint64 endTime)
{
    if (!checkTable())
        return false;
    QSqlQuery query(DATABASE);
    QString strsql = QString("select id,stype,stime,remark from TSystemLog where stime >= %1 and stime <= %2").arg(startTime).arg(endTime);
    query.exec(strsql);
    if (!query.exec())
    {
        DBAPP->showErrorInfo("DbSystemLog::getList", query);
        return false;
    }
    else
    {
        modelList.clear();
        while (query.next())
        {
            QSharedPointer<ModelSystemLog> model(new ModelSystemLog);
            model->id = query.value("id").toInt();
            model->stype = query.value("stype").toInt();
            model->stime = query.value("stime").toLongLong();
            model->remark = query.value("remark").toString();
            modelList.append(model);
        }
        return true;
    }
}

bool DbSystemLog::getList_page(QVector<QSharedPointer<ModelSystemLog>> &modelList, int pageIndex, int pageSize)
{
    if (!checkTable())
        return false;
    QSqlQuery query(DATABASE);
    query.exec(QString("select id,stype,stime,remark from TSystemLog order by stime limit %1,%2").arg(pageIndex * pageSize).arg(pageSize));
    if (!query.exec())
    {
        DBAPP->showErrorInfo("DbSystemLog::getList_page", query);
        return false;
    }
    else
    {
        modelList.clear();
        while (query.next())
        {
            QSharedPointer<ModelSystemLog> model(new ModelSystemLog);
            model->id = query.value("id").toInt();
            model->stype = query.value("stype").toInt();
            model->stime = query.value("stime").toLongLong();
            model->remark = query.value("remark").toString();
            modelList.append(model);
        }
        return true;
    }
}

bool DbSystemLog::checkTable()
{
    if (tableOk)
        return true;
    if (!DBAPP || !DATABASE.isOpen())
        return false;
    QSqlQuery query(DATABASE);
    QString strsql = QString("create table if not exists TSystemLog(ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, stype INTEGER, stime INTEGER, remark VARCHAR(256),posted INTEGER DEFAULT 0);");
    if (!query.exec(strsql))
    {
        DBAPP->showErrorInfo("DbSystemLog::checkTable", query);
        return false;
    }
    tableOk = true;
    return true;
}

bool DbSystemLog::save(qint64 stime, int stype, QString remark, bool posted)
{
    if (!checkTable())
        return false;
    QSqlQuery query(DATABASE);
    query.prepare(QString("insert into TSystemLog(stype,stime,remark,posted) values(%1,%2,:remark,%3)").arg(stype).arg(stime).arg(posted ? 1 : 0));
    query.bindValue(":remark", remark);
    if (!query.exec())
    {
        DBAPP->showErrorInfo("DbSystemLog::save", query);
        return false;
    }
    return true;
}

bool DbSystemLog::removeBefore(qint64 ctime)
{
    if (!DBAPP || !DATABASE.isOpen() || !checkTable())
        return false;
    QSqlQuery query(DATABASE);
    QString strsql = QString("delete from TSystemLog where stime<%1").arg(ctime);
    if (!query.exec(strsql))
    {
        DBAPP->showErrorInfo("DbSystemLog::removeBefore", query);
        return false;
    }
    return true;
}

int DbSystemLog::getCount()
{
    if (!DBAPP || !DATABASE.isOpen() || !checkTable())
        return 0;
    QSqlQuery query(DATABASE);
    query.prepare("select count(*) from TSystemLog");
    if (!query.exec())
    {
        DBAPP->showErrorInfo("DbSystemLog::getCount", query);
        return 0;
    }
    query.next();
    return query.value(0).toInt();
}